package ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import org.janusproject.kernel.address.AgentAddress;

import sma.agents.listeners.DeleteListener;
import sma.agents.listeners.MoveListener;
import appli.utils.Config;
import sma.agents.bodies.CircleBody;

/**
 * C'est le JPanel qui represente les agents dans l'environnement local.
 * 
 * @author Etienne Sainton
 * @version 0.2
 */
public class ConfigPanel extends JPanel {
	

	private static final long serialVersionUID = 8494872063465732463L;
	private AppliWindow frame;
	private GridBagConstraints c;
	
	private JTextField textTelemeterNb;
	private JCheckBox checkDebug;
	private JCheckBox checkVerbose;
	private JTextField textTelemeterDistance;
	private JTextField textTopGap;
	private JTextField textDecisiveNumber;
	private JTextField textPerception;
	private JTextField textResolution;
	
	private JTextField textTelemeterAddress;
	private JTextField textDirectionAddress;
	private JTextField textDirectionPort;
	
	private JCheckBox checkFillHole;
	
	private List<JTextField> listTelemeterPort = new ArrayList<JTextField>();
	private List<JLabel> listTelemeterLabel = new ArrayList<JLabel>();
	
	private ConfigSmaPanel configSMA;
	
	/**
	 * Constructeur.
	 * @param _frame La frame dans laquelle el panel apparait
	 * @param configSMA 
	 */
	public ConfigPanel(AppliWindow _frame, ConfigSmaPanel configSMA) {
		
		frame = _frame;
		this.configSMA = configSMA;
		
		this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5,5,5,5);  //padding
        
		 //Create the two buttons
        JButton startButton = new JButton("Start Demo");
        
        //Create the action listener for button
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                startAction();
            }

        });
        
        c.gridx = 0;
    	c.gridy = 0;
        this.add(new JLabel("Developpement"));
       
        
        c.gridx = 0;
    	c.gridy = 1;
        JLabel lDebug = new JLabel("Debug \t\t");
        this.add(lDebug,c);
        
        c.gridx = 1;
        checkDebug = new JCheckBox();
        checkDebug.setSelected(Config.DEBUG);
        this.add(checkDebug,c);
           	
        JLabel lVerbose = new JLabel("Verbose \t\t");
        c.gridx = 0;
    	c.gridy = 2;
        this.add(lVerbose,c);
        
        checkVerbose = new JCheckBox();
        checkVerbose.setSelected(Config.VERBOSE);
        c.gridx = 1;
    	c.gridy = 2;
    	this.add(checkVerbose,c);
        
    	c.gridx = 0;
    	c.gridy = 3;       
        JLabel lTelemeter = new JLabel("Telemeter distance");
        this.add(lTelemeter,c);
        
        c.gridx = 1;
        textTelemeterDistance = new JTextField(""+Config.TELEMETER_DISTANCE); 
        this.add(textTelemeterDistance,c);
        
        c.gridx = 0;
    	c.gridy = 4; 
        JLabel lTopGap = new JLabel("Top gap");
        this.add(lTopGap,c);
        
        c.gridx = 1;
        textTopGap = new JTextField(""+Config.TOP_GAP); 
        this.add(textTopGap,c);
        
        c.gridx = 0;
    	c.gridy = 5;
        JLabel lFillHole = new JLabel("Fill obstacle hole");
        this.add(lFillHole,c);
        
        c.gridx = 1;
        checkFillHole = new JCheckBox();
        checkFillHole.setSelected(Config.FILL_OBSTACLE_HOLE);
        this.add(checkFillHole,c);
        
        c.gridx = 0;
    	c.gridy = 6;
    	JLabel lTelemeterNb = new JLabel("Telemeter number");
        this.add(lTelemeterNb,c);
        
        c.gridx = 1;
        textTelemeterNb = new JTextField(""+Config.TELEMETER_NUMBER); 
        this.add(textTelemeterNb,c);
        
        c.gridx = 0;
    	c.gridy = 7;
        JLabel lVide = new JLabel("");
        this.add(lVide,c);
        
        c.gridx = 1;
        JButton btnTelemeterNumber = new JButton("Refresh telemeter");
        //Create the action listener for buttons
        btnTelemeterNumber.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                refreshTelemeterField();
                
            }
        });
        
        this.add(btnTelemeterNumber,c);
        
        c.gridx = 2;
    	c.gridy = 0;
        this.add(new JLabel("Multi Agent"),c);
        
    	c.gridx = 2;
    	c.gridy = 1;
        JLabel lResolution = new JLabel("Resolution");
        this.add(lResolution,c);
        
        c.gridx = 3;
        textResolution = new JTextField(""+Config.RESOLUTION);
        this.add(textResolution,c);
        
        c.gridx = 2;
    	c.gridy = 2;
        JLabel lPercpetion = new JLabel("Perception");
        this.add(lPercpetion,c);
        
        c.gridx = 3;
        textPerception = new JTextField(""+Config.PERCEPTION);
        this.add(textPerception,c);
        
        c.gridx = 2;
    	c.gridy = 3;
        JLabel lDecisiveNumber = new JLabel("Decisive Agent number");
        this.add(lDecisiveNumber,c);
        
        c.gridx = 3;
        textDecisiveNumber = new JTextField(""+Config.decisiveAgentNb);
        
        this.add(textDecisiveNumber,c);
        
        c.gridx = 3;
    	c.gridy = 4;
        
    	this.add(startButton,c);
    	
    	refreshTelemeterField();
        
    	this.setVisible(true);
    	
    	this.validate();
    	this.repaint();
       
	}
	
	/**
	 * Permet de changer à la volée le nombre de télémètres utilisés
	 */
	public void refreshTelemeterField() {
		
		int nb = Integer.parseInt(this.textTelemeterNb.getText());
		int X = 4;
		int Y = 4;
		
		if(!listTelemeterPort.isEmpty()){
			for(int i=0; i<listTelemeterPort.size(); i++){
				this.remove(listTelemeterPort.get(i));
				this.remove(listTelemeterLabel.get(i));
			}
		}
			
		
		listTelemeterPort = new ArrayList<JTextField>();
		listTelemeterLabel = new ArrayList<JLabel>();
		
		c.gridx = X+1;
    	c.gridy = 0;
    	
		this.add(new JLabel("Communication"),c);
		
		c.gridx = X;
    	c.gridy = 1;
    	this.add(new JLabel("Direction address"),c);
    	textDirectionAddress = new JTextField(Config.DIRECTION_ADDRESS);
    	    	
    	c.gridx = X+1;
    	this.add(textDirectionAddress,c);
    	
    	c.gridx = X;
    	c.gridy = 2;
    	this.add(new JLabel("Direction port"),c);
    	textDirectionPort = new JTextField(""+Config.DIRECTION_PORT);
    	
    	c.gridx = X+1;
    	this.add(textDirectionPort,c);
    	
    	c.gridx = X;
    	c.gridy = 3;
    	this.add(new JLabel("Telemeter address"),c);
    	textTelemeterAddress = new JTextField(Config.TELEMETER_ADDRESS);
    	
    	c.gridx = X+1;
    	this.add(textTelemeterAddress,c);
		
		for(int i=0; i<nb; i++){
			c.gridx = X;
	    	c.gridy = Y;
	    	listTelemeterLabel.add(new JLabel("Telemeter " + (i+1)));
			this.add(listTelemeterLabel.get(i),c);
			
			c.gridx = X+1;
			listTelemeterPort.add(new JTextField(""+Config.TELEMETER_PORT.get(i)));
			this.add(listTelemeterPort.get(i),c);
			
	        Y++;
		}
		
		
		
		this.revalidate();
		this.repaint();

	}   
	
	/**
	 * Rafraichit toute la config
	 */
	public void startAction() {
		//Refresh all config data
		
		Config.DEBUG = checkDebug.isSelected();
		
		Config.VERBOSE = checkVerbose.isSelected();

		Config.RESOLUTION = Integer.parseInt(textResolution.getText());
		
		Config.PERCEPTION = Integer.parseInt(textPerception.getText());
		
		Config.decisiveAgentNb = Integer.parseInt(textDecisiveNumber.getText());
		
		Config.TELEMETER_NUMBER = Integer.parseInt(textTelemeterNb.getText());
		
		Config.TELEMETER_DISTANCE = Integer.parseInt(textTelemeterDistance.getText());
		
		Config.TOP_GAP = Integer.parseInt(textTopGap.getText());
		
		Config.FILL_OBSTACLE_HOLE = checkFillHole.isSelected();
		
		Config.TELEMETER_PORT = new ArrayList<Integer>(getPortsTelemeter());
		
		Config.TELEMETER_ADDRESS = textTelemeterAddress.getText();
		
		Config.DIRECTION_ADDRESS = textDirectionAddress.getText();
		
		Config.DIRECTION_PORT = Integer.parseInt(textDirectionPort.getText());
		
		Config.SMA_CONSTANTE = configSMA.getConstantes(false);
		
		Config.SMA_CONSTANTE_PARK = configSMA.getConstantes(true);
		
		this.frame.startSimu();
	}

	private List<Integer> getPortsTelemeter() {
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for(JTextField text : listTelemeterPort){
			list.add(Integer.parseInt(text.getText()));
		}
		
		return list;
	}
    

	
}
